package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import Units.Cavalry;
import Units.FootSoldier;
import Units.Scout;
import Units.Tank;
import Units.ValidUnits;
/**
 * panel to selet which unit to build
 * @author Justin Yu
 *
 */
public class UnitSelector {
	Player p;
	resourceArray ra;
	JFrame frame = new JFrame("New Unit");
	JPanel panel = new JPanel();
	ValidUnits v;
	ArrayList<String> valid;
	BasicMap bm;
	MapPanel mp;
	Unit u;
	boolean dead = false;
/**
 * basic constructor
 * @param p1
 * @param map
 * @param mpanel
 * @param x
 * @param y
 * @param px_x
 * @param px_y
 */
	public UnitSelector(Player p1, BasicMap map, MapPanel mpanel, final int x, final int y, final int px_x, final int px_y) {
		p = p1;
		frame.add(panel);
		v = new ValidUnits(p);

		bm = map;
		mp = mpanel;

		JButton cav = new JButton("Cavalry");
		JButton foot = new JButton("Foot Soldier");
		JButton scout = new JButton("Scout");
		JButton tank = new JButton("Tank");

		valid = v.getValid();
		while(!(valid.isEmpty())) {
			if(valid.get(0).equals("Cavalry"))
				panel.add(cav);
			else if(valid.get(0).equals("Foot Soldier"))
				panel.add(foot);
			else if(valid.get(0).equals("Scout"))
				panel.add(scout);
			else if(valid.get(0).equals("Tank"))
				panel.add(tank);
			valid.remove(0);
		}

		cav.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				u = new Cavalry(x, y, p);
				if(bm.getLand(x, y).getUnit() == null) {
					if(checkCost(u)) {
						bm.getLand(x, y).addUnit(u);
						p.addUnit(u);
					}
				} else
					JOptionPane.showMessageDialog(new JFrame(), "Invalid (occupied)");
				dispose();
			}		
		});

		foot.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				u = new FootSoldier(x, y, p);
				if(bm.getLand(x, y).getUnit() == null) {
					if(checkCost(u)) {
						bm.getLand(x, y).addUnit(u);
						p.addUnit(u);
					}
				} else
					JOptionPane.showMessageDialog(new JFrame(), "Invalid (occupied)");
				dispose();
			}		
		});

		scout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				u = new Scout(x, y, p);
				if(bm.getLand(x, y).getUnit() == null) {
					if(checkCost(u)) {
						bm.getLand(x, y).addUnit(u);
						p.addUnit(u);
					}
				} else
					JOptionPane.showMessageDialog(new JFrame(), "Invalid (occupied)");
				dispose();
			}		
		});

		tank.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				u = new Tank(x, y, p);
				if(bm.getLand(x, y).getUnit() == null) {
					if(checkCost(u)) {
						bm.getLand(x, y).addUnit(u);
						p.addUnit(u);
					}
				} else
					JOptionPane.showMessageDialog(new JFrame(), "Invalid (occupied)");
				dispose();
			}		
		});

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.err.println("don't tell anyone, but the user is being an idiot [unit selector cancel]");
				dead = true;
			}
		});

		frame.setLocation(px_x, px_y);
		frame.setVisible(true);
		frame.setSize(300, 75);
	}
	/**
	 * kill the panel
	 */
	public void dispose() {
		dead = true;
		mp.repaint();
		frame.dispose();
	}
/**
 * do a quick cost check
 * @param u unit
 * @return if affordable?
 */
	public boolean checkCost(Unit u) {
		int[] res = p.getRes().getArray();
		int[] cost = u.getCost();

		for(int i = 0; i < res.length; i++) {
			if(res[i] < cost[i]) {
				String tech = "";

				if(i==0)
					tech = "wood";
				else if(i==1)
					tech = "metal";
				else if(i==2)
					tech = "food";
				else if(i==3)
					tech = "happiness";
				else if(i==4)
					tech = "research";

				JOptionPane.showMessageDialog(new JFrame(), ("Not enough resources [" +tech +(res[i]-cost[i]) +"]"));
				return false;
			}
			else res[i] -= cost[i];
		}
		p.getRes().synch();
		return true;
	}
}